package com.aidex.biz.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aidex.biz.domain.PersonInfo;
import com.aidex.common.annotation.DataScope;
import com.aidex.common.exception.CustomException;
import com.aidex.common.utils.StringUtils;
import com.aidex.common.utils.sign.Md5Utils;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.domain.entity.SysUser;
import com.aidex.common.core.domain.entity.SysDept;
import com.aidex.common.core.service.BaseServiceImpl;
import com.aidex.framework.cache.DeptUtils;
import com.aidex.framework.cache.UserUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.aidex.biz.mapper.ZhuanxiangAssessMapper;
import com.aidex.biz.domain.ZhuanxiangAssess;
import com.aidex.biz.service.ZhuanxiangAssessService;
import org.springframework.transaction.annotation.Transactional;
import com.aidex.common.utils.NumberUtils;

/**
 * 专项监督Service业务层处理
 * @author sean
 * @email 2074844820@qq.com
 * @date 2025-03-26
 */
@Service
@Transactional(readOnly = true)
public class ZhuanxiangAssessServiceImpl extends BaseServiceImpl<ZhuanxiangAssessMapper, ZhuanxiangAssess> implements ZhuanxiangAssessService
{
    private static final Logger log = LoggerFactory.getLogger(ZhuanxiangAssessServiceImpl.class);

    /**
     * 获取单条数据
     * @param zhuanxiangAssess 专项监督
     * @return 专项监督
     */
    @Override
    public ZhuanxiangAssess get(ZhuanxiangAssess zhuanxiangAssess)
    {
        ZhuanxiangAssess dto = super.get(zhuanxiangAssess);
        return dto;
    }

    /**
     * 获取单条数据
     * @param id 专项监督id
     * @return 专项监督
     */
    @Override
    public ZhuanxiangAssess get(String id)
    {
        ZhuanxiangAssess dto = super.get(id);
        return dto;
    }


    /**
     * 查询专项监督列表
     * @param zhuanxiangAssess 专项监督
     * @return 专项监督
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public List<ZhuanxiangAssess> findList(ZhuanxiangAssess zhuanxiangAssess)
    {
        return super.findList(zhuanxiangAssess);
    }

    /**
     * 分页查询专项监督列表
     * @param zhuanxiangAssess 专项监督
     * @return 专项监督
     */
    @Override
    @DataScope(userAlias = "a",deptAlias = "a")
    public PageInfo<ZhuanxiangAssess> findPage(ZhuanxiangAssess zhuanxiangAssess)
    {
        return super.findPage(zhuanxiangAssess);
    }

    /**
     * 保存专项监督
     * @param zhuanxiangAssess
     * @return 结果
     */
    @Override
    public boolean save(ZhuanxiangAssess zhuanxiangAssess)
    {
        return super.save(zhuanxiangAssess);
    }


    /**
     * 删除专项监督信息
     * @param zhuanxiangAssess
     * @return 结果
     */
    @Override
    public boolean remove(ZhuanxiangAssess zhuanxiangAssess)
    {
        return super.remove(zhuanxiangAssess);
    }

    /**
     * 批量删除专项监督
     * @param ids 需要删除的专项监督ID
     * @return 结果
     */
    @Transactional(readOnly = false)
    @Override
    public int deleteZhuanxiangAssessByIds(String[] ids)
    {
        return mapper.deleteZhuanxiangAssessByIds(ids);
    }

    /**
     * 获取最大编号
     * @param zhuanxiangAssess 专项监督
     * @return 结果
     */
    @Override
    public int findMaxSort(ZhuanxiangAssess zhuanxiangAssess)
    {
        return NumberUtils.nextOrder(mapper.findMaxSort(zhuanxiangAssess));
    }

    @Override
    @Transactional(readOnly = false)
    public String importInfo(List<ZhuanxiangAssess> zhuanxiangAssess) {
        if (StringUtils.isNull(zhuanxiangAssess) || zhuanxiangAssess.size() == 0)
        {
            throw new CustomException("导入数据不能为空！");
        }
        // 先删除
        mapper.deleteAll();
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZhuanxiangAssess personInfo : zhuanxiangAssess)
        {
            try
            {
                if(StringUtils.isEmpty(personInfo.getBeiJianduDanwei())||StringUtils.isEmpty(personInfo.getJianduDanwei())){
                    continue;
                }
                    personInfo.setIsNewRecord();
                    this.save(personInfo);
                    successNum++;

            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "";
                log.error(msg, e);
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确");
            throw new CustomException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional(readOnly = false)
    public Integer deleteAll() {
        return mapper.deleteAll();
    }

    public List<ZhuanxiangAssess> getZhuanxiangAssessMap(ZhuanxiangAssess zhuanxiangAssess){
        return mapper.findListGroupBy(zhuanxiangAssess);
    }

}
